<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <h2>用户注册</h2>
        <p>请填写用户信息进行注册</p>
      </div>
      
      <div class="register-form">
        <div class="form-group">
          <label for="user_id">用户账号</label>
          <input 
            type="text" 
            id="user_id" 
            v-model="registerForm.user_id" 
            placeholder="请输入用户账号"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="registerForm.password" 
            placeholder="请输入密码"
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input 
            type="password" 
            id="confirmPassword" 
            v-model="confirmPassword" 
            placeholder="请再次输入密码"
          />
        </div>
        
        <div class="form-actions">
          <button 
            type="button" 
            class="register-btn" 
            @click="handleRegister"
            :disabled="loading"
          >
            {{ loading ? '注册中...' : '注册' }}
          </button>
          <button 
            type="button" 
            class="login-link-btn" 
            @click="goToLogin"
          >
            已有账号？去登录
          </button>
        </div>
        
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        
        <div v-if="successMessage" class="success-message">
          {{ successMessage }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI } from '@/api/api'

export default {
  name: 'RegisterView',
  data() {
    return {
      registerForm: {
        user_id: '',
        password: ''
      },
      confirmPassword: '',
      loading: false,
      errorMessage: '',
      successMessage: ''
    }
  },
  methods: {
    async handleRegister() {
      // 表单验证
      if (!this.registerForm.user_id || !this.registerForm.password) {
        this.errorMessage = '请填写所有必填项'
        return
      }
      
      if (this.registerForm.password !== this.confirmPassword) {
        this.errorMessage = '两次输入的密码不一致'
        return
      }
      
      if (this.registerForm.password.length < 6) {
        this.errorMessage = '密码长度至少6位'
        return
      }
      
      this.loading = true
      this.errorMessage = ''
      this.successMessage = ''
      
      try {
        // 注册用户
        const response = await userAPI.add(this.registerForm)
        
        if (response && response.code === 200) {
          const user = response.data
          this.successMessage = `注册成功！，请前往登录页面登录`
          // 清空表单
          this.registerForm = {
            user_id: '',
            password: ''
          }
          this.confirmPassword = ''
          
          // 5秒后跳转到登录页
          setTimeout(() => {
            if (this.$route.path !== '/login') {
              this.$router.push('/login').catch(() => {})
            }
          }, 5000)
        } else {
          this.errorMessage = response.message || '注册失败'
        }
      } catch (error) {
        console.error('注册失败：', error)
        this.errorMessage = '注册失败，请检查网络连接'
      } finally {
        this.loading = false
      }
    },
    
    goToLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-box {
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 450px;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-header h2 {
  color: #333;
  margin-bottom: 8px;
  font-size: 28px;
  font-weight: 600;
}

.register-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.register-form {
  width: 100%;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
}

.form-group input::placeholder {
  color: #999;
}

.form-actions {
  margin-top: 30px;
}

.register-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  margin-bottom: 15px;
}

.register-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.register-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.login-link-btn {
  width: 100%;
  padding: 12px;
  background: transparent;
  color: #667eea;
  border: 2px solid #667eea;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-link-btn:hover {
  background: #667eea;
  color: white;
}

.error-message {
  margin-top: 15px;
  padding: 12px;
  background-color: #fee;
  border: 1px solid #fcc;
  border-radius: 6px;
  color: #c33;
  font-size: 14px;
  text-align: center;
}

.success-message {
  margin-top: 15px;
  padding: 12px;
  background-color: #efe;
  border: 1px solid #cfc;
  border-radius: 6px;
  color: #3c3;
  font-size: 14px;
  text-align: center;
}

@media (max-width: 480px) {
  .register-box {
    padding: 30px 20px;
  }
  
  .register-header h2 {
    font-size: 24px;
  }
}
</style> 